// DialogPatInfo.cpp : implementation file
//

#include "stdafx.h"
#include "ECGWorkStation.h"
#include "DialogPatInfo.h"
#include "..\public\BiocareDB\BiocareDB.h"
#include "..\public\pubfun\inireadwrite\inireadwrite.h"
#include "..\public\d8\dcrf32.h"
#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDialogPatInfo dialog

UINT uReadCardTimer;
int nReadCardScanTimer = 1000;
HANDLE icdev;
unsigned long cardsnr;

CDialogPatInfo::CDialogPatInfo(CWnd* pParent /*=NULL*/)
	: CDialog(CDialogPatInfo::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDialogPatInfo)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}
CDialogPatInfo::CDialogPatInfo(int nDlgType, PATIENT_DATA* pPatData, CWnd* pParent /*=NULL*/)
: CDialog(CDialogPatInfo::IDD, pParent)
{
	_nDlgType = nDlgType;
	m_pPatData = pPatData;
	//(m_pPatData->strPatientID.GetLength() > 0 && m_pPatData->strName.GetLength() > 0) ?	_bEdit = TRUE : _bEdit = FALSE;
	if (m_pPatData->strSex == "0") {
		m_pPatData->strSex.LoadString(IDS_STRING178);
	}else if (m_pPatData->strSex == "1") {
		m_pPatData->strSex.LoadString(IDS_STRING176);
	}else if (m_pPatData->strSex == "2") m_pPatData->strSex.LoadString(IDS_STRING177);

	//if(_bEdit)
	//	(m_pPatData->tmStudyDateTime.GetTime() == 0) ? m_nLevel = LEVEL_PATIENT : LEVEL_STUDY;
}


void CDialogPatInfo::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDialogPatInfo)
	DDX_Control(pDX, IDC_CHECK_WEIGHT, m_chkWeight);
	DDX_Control(pDX, IDC_CHECK_TEL, m_chkTel);
	DDX_Control(pDX, IDC_CHECK_SZYS, m_chkSZYS);
	DDX_Control(pDX, IDC_CHECK_SEX, m_chkSex);
	DDX_Control(pDX, IDC_CHECK_POSTCODE, m_chkPostCode);
	DDX_Control(pDX, IDC_CHECK_MEMO, m_chkMemo);
	DDX_Control(pDX, IDC_CHECK_KS, m_chkKS);
	DDX_Control(pDX, IDC_CHECK_HOSPNO, m_chkHospNo);
	DDX_Control(pDX, IDC_CHECK_HEIGHT, m_chkHeight);
	DDX_Control(pDX, IDC_CHECK_CZYS, m_chkCZYS);
	DDX_Control(pDX, IDC_CHECK_CLINICNO, m_chkClinicNo);
	DDX_Control(pDX, IDC_CHECK_BP, m_chkBP);
	DDX_Control(pDX, IDC_CHECK_BIRTHDAY, m_chkBirthday);
	DDX_Control(pDX, IDC_CHECK_BEDNO, m_chkBedNo);
	DDX_Control(pDX, IDC_CHECK_AGE, m_chkAge);
	DDX_Control(pDX, IDC_CHECK_ADDRESS, m_chkAddress);
	DDX_Control(pDX, IDC_COMBO_SZYS, m_cbSZYS);
	DDX_Control(pDX, IDC_COMBO_KS, m_cbKS);
	DDX_Control(pDX, IDC_COMBO_CZYS, m_cbCZYS);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDialogPatInfo, CDialog)
	//{{AFX_MSG_MAP(CDialogPatInfo)
	ON_BN_CLICKED(IDC_PATIENT_LIST, OnPatientList)
	ON_WM_TIMER()
	ON_WM_DESTROY()
	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_DATETIMEPICKER_BIRTHDAY, OnDatetimechangeDatetimepickerBirthday)
	ON_EN_CHANGE(IDC_EDIT_AGE, OnChangeEditAge)
	ON_BN_CLICKED(IDC_CHECK_SEX, OnCheckSex)
	ON_BN_CLICKED(IDC_CHECK_ADDRESS, OnCheckAddress)
	ON_BN_CLICKED(IDC_CHECK_AGE, OnCheckAge)
	ON_BN_CLICKED(IDC_CHECK_BEDNO, OnCheckBedno)
	ON_BN_CLICKED(IDC_CHECK_BIRTHDAY, OnCheckBirthday)
	ON_BN_CLICKED(IDC_CHECK_BP, OnCheckBp)
	ON_BN_CLICKED(IDC_CHECK_CLINICNO, OnCheckClinicno)
	ON_BN_CLICKED(IDC_CHECK_CZYS, OnCheckCzys)
	ON_BN_CLICKED(IDC_CHECK_HEIGHT, OnCheckHeight)
	ON_BN_CLICKED(IDC_CHECK_HOSPNO, OnCheckHospno)
	ON_BN_CLICKED(IDC_CHECK_KS, OnCheckKs)
	ON_BN_CLICKED(IDC_CHECK_MEMO, OnCheckMemo)
	ON_BN_CLICKED(IDC_CHECK_POSTCODE, OnCheckPostcode)
	ON_BN_CLICKED(IDC_CHECK_SZYS, OnCheckSzys)
	ON_BN_CLICKED(IDC_CHECK_TEL, OnCheckTel)
	ON_BN_CLICKED(IDC_CHECK_WEIGHT, OnCheckWeight)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialogPatInfo message handlers

BOOL CDialogPatInfo::OnInitDialog() 
{
	CDialog::OnInitDialog();
	icdev=0;	

	InitCtrls();
	SetCtrlText();
	if (_nDlgType == PATIENT_INFO_ADD_PATIENT || _nDlgType == PATIENT_INFO_ADD_PATIENTSTUDY) {
		InitCard();
		uReadCardTimer = (UINT)(SetTimer(2, nReadCardScanTimer ,NULL));
	}
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDialogPatInfo::LoadInputInfo()
{
	CString strTemp;
	CIniReadWrite::GetMe()->LoadString(DEF_INPUT_INFO, DEF_INPUT_INFO_ADDRESS, &strTemp);
	if (!strTemp.CompareNoCase("TRUE")) {
		m_chkAddress.SetCheck(TRUE);
		((CEdit*)GetDlgItem(IDC_EDIT_PATIENT_ADDRESS))->EnableWindow(TRUE);
	}else{
		m_chkAddress.SetCheck(FALSE);
		((CEdit*)GetDlgItem(IDC_EDIT_PATIENT_ADDRESS))->EnableWindow(FALSE);
	}

	CIniReadWrite::GetMe()->LoadString(DEF_INPUT_INFO, DEF_INPUT_INFO_AGE, &strTemp);
	if (!strTemp.CompareNoCase("TRUE")) {
		m_chkAge.SetCheck(TRUE);
		((CEdit*)GetDlgItem(IDC_EDIT_AGE))->EnableWindow(TRUE);
	}else{
		m_chkAge.SetCheck(FALSE);
		((CEdit*)GetDlgItem(IDC_EDIT_AGE))->EnableWindow(FALSE);
	}

	CIniReadWrite::GetMe()->LoadString(DEF_INPUT_INFO, DEF_INPUT_INFO_BEDNO, &strTemp);
	if (!strTemp.CompareNoCase("TRUE")) {
		m_chkBedNo.SetCheck(TRUE);
		((CEdit*)GetDlgItem(IDC_EDIT_BEDNO))->EnableWindow(TRUE);
	}else{
		m_chkBedNo.SetCheck(FALSE);
		((CEdit*)GetDlgItem(IDC_EDIT_BEDNO))->EnableWindow(FALSE);
	}

	CIniReadWrite::GetMe()->LoadString(DEF_INPUT_INFO, DEF_INPUT_INFO_BIRTHDAY, &strTemp);
	if (!strTemp.CompareNoCase("TRUE")) {
		m_chkBirthday.SetCheck(TRUE);
		((CEdit*)GetDlgItem(IDC_DATETIMEPICKER_BIRTHDAY))->EnableWindow(TRUE);
	}else{
		m_chkBirthday.SetCheck(FALSE);
		((CEdit*)GetDlgItem(IDC_DATETIMEPICKER_BIRTHDAY))->EnableWindow(FALSE);
	}

	CIniReadWrite::GetMe()->LoadString(DEF_INPUT_INFO, DEF_INPUT_INFO_BP, &strTemp);
	if (!strTemp.CompareNoCase("TRUE")) {
		m_chkBP.SetCheck(TRUE);
		((CEdit*)GetDlgItem(IDC_EDIT_BP))->EnableWindow(TRUE);
	}else{
		m_chkBP.SetCheck(FALSE);
		((CEdit*)GetDlgItem(IDC_EDIT_BP))->EnableWindow(FALSE);
	}

	CIniReadWrite::GetMe()->LoadString(DEF_INPUT_INFO, DEF_INPUT_INFO_CLINICNO, &strTemp);
	if (!strTemp.CompareNoCase("TRUE")) {
		m_chkClinicNo.SetCheck(TRUE);
		((CEdit*)GetDlgItem(IDC_EDIT_CLINICNO))->EnableWindow(TRUE);
	}else{
		m_chkClinicNo.SetCheck(FALSE);
		((CEdit*)GetDlgItem(IDC_EDIT_CLINICNO))->EnableWindow(FALSE);
	}
	
	CIniReadWrite::GetMe()->LoadString(DEF_INPUT_INFO, DEF_INPUT_INFO_CZYS, &strTemp);
	if (!strTemp.CompareNoCase("TRUE")) {
		m_chkCZYS.SetCheck(TRUE);
		((CEdit*)GetDlgItem(IDC_COMBO_CZYS))->EnableWindow(TRUE);
	}else{
		m_chkCZYS.SetCheck(FALSE);
		((CEdit*)GetDlgItem(IDC_COMBO_CZYS))->EnableWindow(FALSE);
	}

	CIniReadWrite::GetMe()->LoadString(DEF_INPUT_INFO, DEF_INPUT_INFO_HEIGHT, &strTemp);
	if (!strTemp.CompareNoCase("TRUE")) {
		m_chkHeight.SetCheck(TRUE);
		((CEdit*)GetDlgItem(IDC_EDIT_HEIGHT))->EnableWindow(TRUE);
	}else{
		m_chkHeight.SetCheck(FALSE);
		((CEdit*)GetDlgItem(IDC_EDIT_HEIGHT))->EnableWindow(FALSE);
	}

	CIniReadWrite::GetMe()->LoadString(DEF_INPUT_INFO, DEF_INPUT_INFO_HOSPNO, &strTemp);
	if (!strTemp.CompareNoCase("TRUE")) {
		m_chkHospNo.SetCheck(TRUE);
		((CEdit*)GetDlgItem(IDC_EDIT_HOSPNO))->EnableWindow(TRUE);
	}else{
		m_chkHospNo.SetCheck(FALSE);
		((CEdit*)GetDlgItem(IDC_EDIT_HOSPNO))->EnableWindow(FALSE);
	}

	CIniReadWrite::GetMe()->LoadString(DEF_INPUT_INFO, DEF_INPUT_INFO_KS, &strTemp);
	if (!strTemp.CompareNoCase("TRUE")) {
		m_chkKS.SetCheck(TRUE);
		((CEdit*)GetDlgItem(IDC_COMBO_KS))->EnableWindow(TRUE);
	}else{
		m_chkKS.SetCheck(FALSE);
		((CEdit*)GetDlgItem(IDC_COMBO_KS))->EnableWindow(FALSE);
	}

	CIniReadWrite::GetMe()->LoadString(DEF_INPUT_INFO, DEF_INPUT_INFO_MEMO, &strTemp);
	if (!strTemp.CompareNoCase("TRUE")) {
		m_chkMemo.SetCheck(TRUE);
		((CEdit*)GetDlgItem(IDC_EDIT_MEMO))->EnableWindow(TRUE);
	}else{
		m_chkMemo.SetCheck(FALSE);
		((CEdit*)GetDlgItem(IDC_EDIT_MEMO))->EnableWindow(FALSE);
	}

	CIniReadWrite::GetMe()->LoadString(DEF_INPUT_INFO, DEF_INPUT_INFO_POSTCODE, &strTemp);
	if (!strTemp.CompareNoCase("TRUE")) {
		m_chkPostCode.SetCheck(TRUE);
		((CEdit*)GetDlgItem(IDC_EDIT_PATIENT_POSTCODE))->EnableWindow(TRUE);
	}else{
		m_chkPostCode.SetCheck(FALSE);
		((CEdit*)GetDlgItem(IDC_EDIT_PATIENT_POSTCODE))->EnableWindow(FALSE);
	}

	CIniReadWrite::GetMe()->LoadString(DEF_INPUT_INFO, DEF_INPUT_INFO_SEX, &strTemp);
	if (!strTemp.CompareNoCase("TRUE")) {
		m_chkSex.SetCheck(TRUE);
		((CEdit*)GetDlgItem(IDC_COMBO_SEX))->EnableWindow(TRUE);
	}else{
		m_chkSex.SetCheck(FALSE);
		((CEdit*)GetDlgItem(IDC_COMBO_SEX))->EnableWindow(FALSE);
	}

	CIniReadWrite::GetMe()->LoadString(DEF_INPUT_INFO, DEF_INPUT_INFO_SZYS, &strTemp);
	if (!strTemp.CompareNoCase("TRUE")) {
		m_chkSZYS.SetCheck(TRUE);
		((CEdit*)GetDlgItem(IDC_COMBO_SZYS))->EnableWindow(TRUE);
	}else{
		m_chkSZYS.SetCheck(FALSE);
		((CEdit*)GetDlgItem(IDC_COMBO_SZYS))->EnableWindow(FALSE);
	}

	CIniReadWrite::GetMe()->LoadString(DEF_INPUT_INFO, DEF_INPUT_INFO_TEL, &strTemp);
	if (!strTemp.CompareNoCase("TRUE")) {
		m_chkTel.SetCheck(TRUE);
		((CEdit*)GetDlgItem(IDC_EDIT_TEL))->EnableWindow(TRUE);
	}else{
		m_chkTel.SetCheck(FALSE);
		((CEdit*)GetDlgItem(IDC_EDIT_TEL))->EnableWindow(FALSE);
	}

	CIniReadWrite::GetMe()->LoadString(DEF_INPUT_INFO, DEF_INPUT_INFO_WEIGHT, &strTemp);
	if (!strTemp.CompareNoCase("TRUE")) {
		m_chkWeight.SetCheck(TRUE);
		((CEdit*)GetDlgItem(IDC_EDIT_WEIGHT))->EnableWindow(TRUE);
	}else{
		m_chkWeight.SetCheck(FALSE);
		((CEdit*)GetDlgItem(IDC_EDIT_WEIGHT))->EnableWindow(FALSE);
	}

}
void CDialogPatInfo::SaveInputInfo()
{
	m_chkAddress.GetCheck() ? CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_ADDRESS, "TRUE") : CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_ADDRESS, "FALSE");
	m_chkAge.GetCheck() ? CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_AGE, "TRUE") : CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_AGE, "FALSE");
	m_chkBedNo.GetCheck() ? CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_BEDNO, "TRUE") : CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_BEDNO, "FALSE");
	m_chkBirthday.GetCheck() ? CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_BIRTHDAY, "TRUE") : CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_BIRTHDAY, "FALSE");
	m_chkBP.GetCheck() ? CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_BP, "TRUE") : CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_BP, "FALSE");
	m_chkClinicNo.GetCheck() ? CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_CLINICNO, "TRUE") : CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_CLINICNO, "FALSE");
	m_chkCZYS.GetCheck() ? CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_CZYS, "TRUE") : CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_CZYS, "FALSE");
	m_chkHeight.GetCheck() ? CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_HEIGHT, "TRUE") : CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_HEIGHT, "FALSE");
	m_chkHospNo.GetCheck() ? CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_HOSPNO, "TRUE") : CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_HOSPNO, "FALSE");
	m_chkKS.GetCheck() ? CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_KS, "TRUE") : CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_KS, "FALSE");
	m_chkMemo.GetCheck() ? CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_MEMO, "TRUE") : CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_MEMO, "FALSE");
	m_chkPostCode.GetCheck() ? CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_POSTCODE, "TRUE") : CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_POSTCODE, "FALSE");
	m_chkSex.GetCheck() ? CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_SEX, "TRUE") : CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_SEX, "FALSE");
	m_chkSZYS.GetCheck() ? CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_SZYS, "TRUE") : CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_SZYS, "FALSE");
	m_chkTel.GetCheck() ? CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_TEL, "TRUE") : CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_TEL, "FALSE");
	m_chkWeight.GetCheck() ? CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_WEIGHT, "TRUE") : CIniReadWrite::GetMe()->SaveString(DEF_INPUT_INFO, DEF_INPUT_INFO_WEIGHT, "FALSE");
}
void CDialogPatInfo::InitCtrls()
{
	CString strTemp, strField;
	int nNum = 0;
	CIniReadWrite::GetMe()->LoadString(DEF_STUDY_DEPART, DEF_STUDY_DEPART_NUM, &strTemp);
	strTemp.TrimLeft(); strTemp.TrimRight();
	if(strTemp.GetLength() > 0)
		nNum = atoi((LPCTSTR)strTemp);

	strTemp = "";
	for (int nLoop = 0; nLoop < nNum; ++nLoop)
	{
		strField.Format("%s_%d", DEF_STUDY_DEPART_NAME, nLoop);
		CIniReadWrite::GetMe()->LoadString(DEF_STUDY_DEPART, strField, &strTemp);
		m_cbKS.AddString(strTemp);
		strTemp = "";
	}

	nNum = 0;
	CIniReadWrite::GetMe()->LoadString(DEF_STUDY_DR, DEF_STUDY_DR_NUM, &strTemp);
	strTemp.TrimLeft(); strTemp.TrimRight();
	if(strTemp.GetLength() > 0)
		nNum = atoi((LPCTSTR)strTemp);
	
	strTemp = "";
	for (nLoop = 0; nLoop < nNum; ++nLoop)
	{
		strField.Format("%s_%d", DEF_STUDY_DR_NAME, nLoop);
		CIniReadWrite::GetMe()->LoadString(DEF_STUDY_DR, strField, &strTemp);
		m_cbSZYS.AddString(strTemp);
		strTemp = "";
	}

	nNum = 0;
	CIniReadWrite::GetMe()->LoadString(DEF_STUDY_PHYSICAL, DEF_STUDY_PHYSICAL_NUM, &strTemp);
	strTemp.TrimLeft(); strTemp.TrimRight();
	if(strTemp.GetLength() > 0)
		nNum = atoi((LPCTSTR)strTemp);
	
	strTemp = "";
	for (nLoop = 0; nLoop < nNum; ++nLoop)
	{
		strField.Format("%s_%d", DEF_STUDY_PHYSICAL_NAME, nLoop);
		CIniReadWrite::GetMe()->LoadString(DEF_STUDY_PHYSICAL, strField, &strTemp);
		m_cbCZYS.AddString(strTemp);
		strTemp = "";
	}

	LoadInputInfo();
}
void CDialogPatInfo::SetCtrlText(void)
{
	if(m_pPatData == NULL) return;

	char strBuf[1024]={0};

	CECGWorkStationApp* pApp = (CECGWorkStationApp*)AfxGetApp();
	CString strSexF = "", strSexM = "", strSexO = "";
	strSexF.LoadString(IDS_STRING177);
	strSexM.LoadString(IDS_STRING176);
	strSexO.LoadString(IDS_STRING178);
	switch(_nDlgType) {
	case PATIENT_INFO_ADD_PATIENTSTUDY:
		{
			if(m_pPatData->strPatientKey.GetLength() <= 0) m_pPatData->strPatientKey = theApp.GetUID(T_PATIENT);

			if (m_pPatData->strName.GetLength() > 0) 
			{
				 
				}

			if (m_pPatData->strPatientID.GetLength() > 0) 
			{
				//if(!pApp->m_strLanguage.CompareNoCase("RUS"))
				//{
				//	memset(strBuf,0,sizeof(strBuf));
				//	((CEdit*)GetDlgItem(IDC_EDIT_CHNO))->SetWindowText(GBK_to_1251((LPTSTR)(LPCTSTR)m_pPatData->strPatientID,strBuf));
				//}
				//else
					((CEdit*)GetDlgItem(IDC_EDIT_CHNO))->SetWindowText( m_pPatData->strPatientID);
 
			}
			else 
				((CEdit*)GetDlgItem(IDC_EDIT_CHNO))->SetWindowText(theApp.GetUID(T_PATIENT));
			

			if (m_pPatData->strName.GetLength() > 0) 
			{
				 
				if(!pApp->m_strLanguage.CompareNoCase("RUS"))
				{
					memset(strBuf,0,sizeof(strBuf));
					((CEdit*)GetDlgItem(IDC_EDIT_PATNAME))->SetWindowText(GBK_to_1251((LPTSTR)(LPCTSTR)m_pPatData->strName,strBuf));
				}
				else
					((CEdit*)GetDlgItem(IDC_EDIT_PATNAME))->SetWindowText(m_pPatData->strName);
			}
			else 
				((CEdit*)GetDlgItem(IDC_EDIT_PATNAME))->SetWindowText("Anonymous ");
			

			if (m_pPatData->strAge.GetLength() > 0)
			{
 				((CEdit*)GetDlgItem(IDC_EDIT_AGE))->SetWindowText(m_pPatData->strAge);
			}
			if (m_pPatData->strHeight.GetLength() > 0) {
				//((CEdit*)GetDlgItem(IDC_EDIT_AGE))->SetWindowText(theApp.GetUID(T_PATIENT));
				((CEdit*)GetDlgItem(IDC_EDIT_HEIGHT))->SetWindowText(m_pPatData->strHeight);
			}
			if (m_pPatData->strWeight.GetLength() > 0) {
				//((CEdit*)GetDlgItem(IDC_EDIT_AGE))->SetWindowText(theApp.GetUID(T_PATIENT));
				((CEdit*)GetDlgItem(IDC_EDIT_WEIGHT))->SetWindowText(m_pPatData->strWeight);
			}
			if (m_pPatData->strBP.GetLength() > 0) {
				//((CEdit*)GetDlgItem(IDC_EDIT_AGE))->SetWindowText(theApp.GetUID(T_PATIENT));
				int nBP = atoi((LPCTSTR)m_pPatData->strBP);
				int nP = nBP & 0x0000FFFF;
				int nB = nBP >> 16;
				CString strTempBP = "";
				strTempBP.Format("%d.%d", nB, nP);
				((CEdit*)GetDlgItem(IDC_EDIT_BP))->SetWindowText(strTempBP);
			}
			if (m_pPatData->strSex.GetLength() > 0) {
				if (m_pPatData->strSex == "0" || m_pPatData->strSex == strSexO) {
					((CComboBox*)GetDlgItem(IDC_COMBO_SEX))->SetCurSel(2);
				}else if(m_pPatData->strSex == "1" || m_pPatData->strSex == strSexM){
					((CComboBox*)GetDlgItem(IDC_COMBO_SEX))->SetCurSel(1);
				}else{
					((CComboBox*)GetDlgItem(IDC_COMBO_SEX))->SetCurSel(0);
				}
			}else ((CComboBox*)GetDlgItem(IDC_COMBO_SEX))->SetCurSel(0);
			((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_BIRTHDAY))->SetTime(m_pPatData->tmBirthday);
		}
		break;
	case PATIENT_INFO_ADD_PATIENT:
		{
			((CEdit*)GetDlgItem(IDC_EDIT_CLINICNO))->EnableWindow(FALSE);
			((CEdit*)GetDlgItem(IDC_EDIT_HOSPNO))->EnableWindow(FALSE);
			((CEdit*)GetDlgItem(IDC_EDIT_BEDNO))->EnableWindow(FALSE);
			((CEdit*)GetDlgItem(IDC_EDIT_MEMO))->EnableWindow(FALSE);
			((CButton*)GetDlgItem(IDC_PATIENT_LIST))->EnableWindow(FALSE);
			((CComboBox*)GetDlgItem(IDC_COMBO_SEX))->SetCurSel(0);

			((CEdit*)GetDlgItem(IDC_EDIT_CHNO))->SetWindowText(theApp.GetUID(T_PATIENT));
			((CEdit*)GetDlgItem(IDC_EDIT_PATNAME))->SetWindowText("Anonymous ");
		}
		break;
	case PATIENT_INFO_EDIT_PATIENT:
		{
			if(m_pPatData->strName.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_PATNAME))->SetWindowText(m_pPatData->strName);
			if(m_pPatData->strPatientID.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_CHNO))->SetWindowText(m_pPatData->strPatientID);

			if(m_pPatData->strAge.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_AGE))->SetWindowText(m_pPatData->strAge);
			if(m_pPatData->strHeight.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_HEIGHT))->SetWindowText(m_pPatData->strHeight);
			if(m_pPatData->strWeight.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_WEIGHT))->SetWindowText(m_pPatData->strWeight);
			if(m_pPatData->strBP.GetLength() > 0){
				int nBP = atoi((LPCTSTR)m_pPatData->strBP);
				int nP = nBP & 0x0000FFFF;
				int nB = nBP >> 16;
				CString strTempBP = "";
				strTempBP.Format("%d.%d", nB, nP);

				((CEdit*)GetDlgItem(IDC_EDIT_BP))->SetWindowText(strTempBP);
			}
			if(m_pPatData->strClinicNo.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_CLINICNO))->SetWindowText(m_pPatData->strClinicNo);

			if(m_pPatData->strHospNo.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_HOSPNO))->SetWindowText(m_pPatData->strHospNo);

			if(m_pPatData->strBedNo.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_BEDNO))->SetWindowText(m_pPatData->strBedNo);

			if(m_pPatData->strMemo.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_MEMO))->SetWindowText(m_pPatData->strMemo);

			if(m_pPatData->strPhysical.GetLength() > 0)
				((CComboBox*)GetDlgItem(IDC_COMBO_CZYS))->SetWindowText(m_pPatData->strPhysical);
			if(m_pPatData->strDR.GetLength() > 0)
				((CComboBox*)GetDlgItem(IDC_COMBO_SZYS))->SetWindowText(m_pPatData->strDR);
			if(m_pPatData->strDepart.GetLength() > 0)
				((CComboBox*)GetDlgItem(IDC_COMBO_KS))->SetWindowText(m_pPatData->strDepart);

			if(m_pPatData->strTel.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_TEL))->SetWindowText(m_pPatData->strTel);

			if(m_pPatData->strPostCode.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_PATIENT_POSTCODE))->SetWindowText(m_pPatData->strPostCode);

			if(m_pPatData->strAddress.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_PATIENT_ADDRESS))->SetWindowText(m_pPatData->strAddress);

			((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_BIRTHDAY))->SetTime(m_pPatData->tmBirthday);

			((CButton*)GetDlgItem(IDC_PATIENT_LIST))->EnableWindow(FALSE);

			if (m_pPatData->strSex == strSexF) {
				((CComboBox*)GetDlgItem(IDC_COMBO_SEX))->SetCurSel(0);
			}else if (m_pPatData->strSex == strSexM) {
				((CComboBox*)GetDlgItem(IDC_COMBO_SEX))->SetCurSel(1);
			}else if (m_pPatData->strSex == strSexO) {
				((CComboBox*)GetDlgItem(IDC_COMBO_SEX))->SetCurSel(2);
			}

//			((CEdit*)GetDlgItem(IDC_EDIT_CHNO))->EnableWindow(FALSE);
//			((CEdit*)GetDlgItem(IDC_EDIT_CLINICNO))->EnableWindow(FALSE);
//			((CEdit*)GetDlgItem(IDC_EDIT_HOSPNO))->EnableWindow(FALSE);
//			((CEdit*)GetDlgItem(IDC_EDIT_BEDNO))->EnableWindow(FALSE);
			((CEdit*)GetDlgItem(IDC_EDIT_MEMO))->EnableWindow(FALSE);
		}
		break;
	case PATIENT_INFO_EDIT_STUDY:
	case PATIENT_INFO_ADD_STUDY:
		{
			if(m_pPatData->strName.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_PATNAME))->SetWindowText(m_pPatData->strName);
			if(m_pPatData->strPatientID.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_CHNO))->SetWindowText(m_pPatData->strPatientID);
			if(m_pPatData->strClinicNo.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_CLINICNO))->SetWindowText(m_pPatData->strClinicNo);
			if(m_pPatData->strAge.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_AGE))->SetWindowText(m_pPatData->strAge);

			if(m_pPatData->strHeight.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_HEIGHT))->SetWindowText(m_pPatData->strHeight);
			if(m_pPatData->strWeight.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_WEIGHT))->SetWindowText(m_pPatData->strWeight);
			if(m_pPatData->strBP.GetLength() > 0){
				int nBP = atoi((LPCTSTR)m_pPatData->strBP);
				int nP = nBP & 0x0000FFFF;
				int nB = nBP >> 16;
				CString strTempBP = "";
				strTempBP.Format("%d.%d", nB, nP);

				((CEdit*)GetDlgItem(IDC_EDIT_BP))->SetWindowText(strTempBP);
			}

			if(m_pPatData->strHospNo.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_HOSPNO))->SetWindowText(m_pPatData->strHospNo);

			if(m_pPatData->strBedNo.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_BEDNO))->SetWindowText(m_pPatData->strBedNo);

			if(m_pPatData->strMemo.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_MEMO))->SetWindowText(m_pPatData->strMemo);
			if(m_pPatData->strTel.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_TEL))->SetWindowText(m_pPatData->strTel);

			if(m_pPatData->strPhysical.GetLength() > 0)
				((CComboBox*)GetDlgItem(IDC_COMBO_CZYS))->SetWindowText(m_pPatData->strPhysical);
			if(m_pPatData->strDR.GetLength() > 0)
				((CComboBox*)GetDlgItem(IDC_COMBO_SZYS))->SetWindowText(m_pPatData->strDR);
			if(m_pPatData->strPhysical.GetLength() > 0)
				((CComboBox*)GetDlgItem(IDC_COMBO_KS))->SetWindowText(m_pPatData->strDepart);

			if(m_pPatData->strPostCode.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_PATIENT_POSTCODE))->SetWindowText(m_pPatData->strPostCode);

			if(m_pPatData->strAddress.GetLength() > 0)
				((CEdit*)GetDlgItem(IDC_EDIT_PATIENT_ADDRESS))->SetWindowText(m_pPatData->strAddress);
			if (_nDlgType == PATIENT_INFO_ADD_STUDY) {
				((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_BIRTHDAY))->SetTime(COleDateTime::GetCurrentTime());
			}else{
				((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_BIRTHDAY))->SetTime(m_pPatData->tmBirthday);
			}

			((CButton*)GetDlgItem(IDC_PATIENT_LIST))->EnableWindow(FALSE);

			if (m_pPatData->strSex == strSexF) {
				((CComboBox*)GetDlgItem(IDC_COMBO_SEX))->SetCurSel(0);
			}else if (m_pPatData->strSex == strSexM) {
				((CComboBox*)GetDlgItem(IDC_COMBO_SEX))->SetCurSel(1);
			}else if (m_pPatData->strSex == strSexO) {
				((CComboBox*)GetDlgItem(IDC_COMBO_SEX))->SetCurSel(2);
			}
			((CEdit*)GetDlgItem(IDC_EDIT_CHNO))->EnableWindow(FALSE);
			((CEdit*)GetDlgItem(IDC_EDIT_PATNAME))->EnableWindow(FALSE);
			((CEdit*)GetDlgItem(IDC_EDIT_TEL))->EnableWindow(FALSE);
			((CEdit*)GetDlgItem(IDC_EDIT_PATIENT_POSTCODE))->EnableWindow(FALSE);
			((CEdit*)GetDlgItem(IDC_EDIT_PATIENT_ADDRESS))->EnableWindow(FALSE);
			((CEdit*)GetDlgItem(IDC_DATETIMEPICKER_BIRTHDAY))->EnableWindow(FALSE);
			((CEdit*)GetDlgItem(IDC_COMBO_SEX))->EnableWindow(FALSE);
		}
	break;
	default:
		break;
	}
}

BOOL CDialogPatInfo::BuildPatientData(void)
{
	CString strTemp = "";
	((CEdit*)GetDlgItem(IDC_EDIT_PATNAME))->GetWindowText(strTemp);
//	if(strTemp.GetLength() <= 0) return FALSE;
	m_pPatData->strName = strTemp;

	((CEdit*)GetDlgItem(IDC_EDIT_CHNO))->GetWindowText(strTemp);
	if(strTemp.GetLength() <= 0) return FALSE;
	m_pPatData->strPatientID = strTemp;

	CString strSexF = "", strSexM = "", strSexO = "";
	strSexF.LoadString(IDS_STRING177);
	strSexM.LoadString(IDS_STRING176);
	strSexO.LoadString(IDS_STRING178);
	switch(((CComboBox*)GetDlgItem(IDC_COMBO_SEX))->GetCurSel()){
		case 0:
			m_pPatData->strSex = strSexF;
			break;
		case 1:
			m_pPatData->strSex = strSexM;
			break;
		case 2:
			m_pPatData->strSex = strSexO;
			break;
		default:
			m_pPatData->strSex = strSexO;
			break;
	}

	((CEdit*)GetDlgItem(IDC_EDIT_CLINICNO))->GetWindowText(strTemp);
	m_pPatData->strClinicNo = strTemp;

	((CEdit*)GetDlgItem(IDC_EDIT_HOSPNO))->GetWindowText(strTemp);
	m_pPatData->strHospNo = strTemp;

	((CEdit*)GetDlgItem(IDC_EDIT_BEDNO))->GetWindowText(strTemp);
	m_pPatData->strBedNo = strTemp;

	((CComboBox*)GetDlgItem(IDC_COMBO_CZYS))->GetWindowText(strTemp);
	m_pPatData->strPhysical = strTemp;
	((CComboBox*)GetDlgItem(IDC_COMBO_SZYS))->GetWindowText(strTemp);
	m_pPatData->strDR = strTemp;
	((CComboBox*)GetDlgItem(IDC_COMBO_KS))->GetWindowText(strTemp);
	m_pPatData->strDepart = strTemp;

	((CEdit*)GetDlgItem(IDC_EDIT_TEL))->GetWindowText(strTemp);
	m_pPatData->strTel = strTemp;

	((CEdit*)GetDlgItem(IDC_EDIT_WEIGHT))->GetWindowText(strTemp);
	m_pPatData->strWeight = strTemp;
	
	((CEdit*)GetDlgItem(IDC_EDIT_HEIGHT))->GetWindowText(strTemp);
	m_pPatData->strHeight = strTemp;
	
	((CEdit*)GetDlgItem(IDC_EDIT_BP))->GetWindowText(strTemp);
	int nB, nP, nPosDot;
	nPosDot = strTemp.Find(".");
	if (nPosDot > 0){
		nB = atoi((LPCTSTR)strTemp.Left(nPosDot));
		nP = atoi((LPCTSTR)strTemp.Right(strTemp.GetLength() - nPosDot - 1));
		strTemp.Format("%d", (nB << 16 | nP));
		m_pPatData->strBP = strTemp;
	}
	
	((CEdit*)GetDlgItem(IDC_EDIT_PATIENT_POSTCODE))->GetWindowText(strTemp);
	m_pPatData->strPostCode = strTemp;

	((CEdit*)GetDlgItem(IDC_EDIT_PATIENT_ADDRESS))->GetWindowText(strTemp);
	m_pPatData->strAddress = strTemp;

//	((CEdit*)GetDlgItem(IDC_EDIT_MEMO))->GetWindowText(strTemp);
//	m_pPatData->strMemo = strTemp;
	
	COleDateTime tmBirthday;
	((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_BIRTHDAY))->GetTime(tmBirthday);
	m_pPatData->tmBirthday = tmBirthday;
	
	COleDateTime tmCurrent = COleDateTime::GetCurrentTime();
	m_pPatData->strAge.Format("%d", tmCurrent.GetYear() - tmBirthday.GetYear());
	
//	if(_nDlgType == PATIENT_INFO_ADD_PATIENT || _nDlgType == PATIENT_INFO_ADD_STUDY || _nDlgType == PATIENT_INFO_ADD_PATIENTSTUDY)
		m_pPatData->tmStudyDateTime = tmCurrent;
	
	if (m_pPatData->strStudyID.GetLength() <= 0) {
		m_pPatData->strStudyID = theApp.GetUID(T_STUDY);
	}
	
	return TRUE;
}

#include "DialogQuery.h"
void CDialogPatInfo::OnPatientList() 
{
	PATIENT_DATA patData;
	InitPatientData(&patData);
	CString strPatientID = "";
	((CEdit*)GetDlgItem(IDC_EDIT_CHNO))->GetWindowText(strPatientID);

	if (strPatientID.GetLength() > 0) {
		patData.strPatientID = strPatientID;
	}

	CString strSexF = "", strSexM = "", strSexO = "";
	strSexF.LoadString(IDS_STRING177);
	strSexM.LoadString(IDS_STRING176);
	strSexO.LoadString(IDS_STRING178);
	CDialogQuery dlg(&patData, this, QUERY_TYPE_GET_PATINFO);
	if (dlg.DoModal() == IDOK) {
		((CEdit*)GetDlgItem(IDC_EDIT_CHNO))->SetWindowText(patData.strPatientID);
		((CEdit*)GetDlgItem(IDC_EDIT_PATNAME))->SetWindowText(patData.strName);
		if (patData.strSex == strSexF) {
			((CComboBox*)GetDlgItem(IDC_COMBO_SEX))->SetCurSel(0);
		}else if (patData.strSex == strSexM) {
			((CComboBox*)GetDlgItem(IDC_COMBO_SEX))->SetCurSel(1);
		}else if (patData.strSex == strSexO) {
			((CComboBox*)GetDlgItem(IDC_COMBO_SEX))->SetCurSel(2);
		}
		((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_BIRTHDAY))->SetTime(patData.tmBirthday);
		((CEdit*)GetDlgItem(IDC_EDIT_TEL))->SetWindowText(patData.strTel);
		((CEdit*)GetDlgItem(IDC_EDIT_PATIENT_POSTCODE))->SetWindowText(patData.strPostCode);
		((CEdit*)GetDlgItem(IDC_EDIT_PATIENT_ADDRESS))->SetWindowText(patData.strAddress);
	}
}

BOOL CDialogPatInfo::SavePatient()
{
	BeginWaitCursor();
	char szErrorMsg[1024];
	memset(szErrorMsg, 0, 1024);
	BOOL bRet = FALSE;
	BCDBConnection DBConn;
	if(!BCDBConnectDB(&DBConn, (LPCTSTR)theApp.m_strConnection, (LPCTSTR)theApp.m_strConnUserName, (LPCTSTR)theApp.m_strConnPassWord, szErrorMsg)) return FALSE;
	BCDBDataSet* pDataSet = NULL;
	BCDBCreateDataSet(&pDataSet, szErrorMsg);
	BCDBDataElem* pDataElem = NULL;

	CString strBirthday = "", strAge = "", strEDOC = "";
	strBirthday.Format("%04d%02d%02d", m_pPatData->tmBirthday.GetYear(), m_pPatData->tmBirthday.GetMonth(), m_pPatData->tmBirthday.GetDay());
	int nAge = 0;
	COleDateTime tmCurrent = COleDateTime::GetCurrentTime();
	nAge = tmCurrent.GetYear() - m_pPatData->tmBirthday.GetYear();
	strAge.Format("%d", nAge);

	if(m_pPatData->strPatientKey.GetLength() <= 0) 
		m_pPatData->strPatientKey = theApp.GetUID(T_PATIENT);
	if (m_pPatData->strStudyID.GetLength() <= 0) {
		m_pPatData->strStudyID = theApp.GetUID(T_STUDY);
	}
	CString strStudyID = m_pPatData->strStudyID;
	CString strStudyDate = "", strStudyTime = "";
	strStudyDate.Format("%04d%02d%02d", m_pPatData->tmStudyDateTime.GetYear(), m_pPatData->tmStudyDateTime.GetMonth(), m_pPatData->tmStudyDateTime.GetDay());
	strStudyTime.Format("%02d%02d%02d", m_pPatData->tmStudyDateTime.GetHour(), m_pPatData->tmStudyDateTime.GetMinute(), m_pPatData->tmStudyDateTime.GetSecond());
	if (_nDlgType == PATIENT_INFO_EDIT_PATIENT) {
		BCDBCreateDataElem(&pDataElem, T_PATIENT_ID, m_pPatData->strPatientID.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strPatientID, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_PATIENT_NAME, m_pPatData->strName.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strName, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_PATIENT_BIRTHDAY, strBirthday.GetLength(), DT_DATE, (void*)(LPCTSTR)strBirthday, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_PATIENT_AGE, strAge.GetLength(), DT_STRING, (void*)(LPCTSTR)strAge, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_PATIENT_WEIGHT, m_pPatData->strWeight.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strWeight, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_PATIENT_HEIGHT, m_pPatData->strHeight.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strHeight, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_PATIENT_BP, m_pPatData->strBP.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strBP, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_PATIENT_TEL, m_pPatData->strTel.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strTel, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_PATIENT_ADDRESS, m_pPatData->strAddress.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strAddress, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_PATIENT_POSTCODE, m_pPatData->strPostCode.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strPostCode, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_PATIENT_SEX, m_pPatData->strSex.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strSex, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		bRet = BCDBUpdateDBData(&DBConn, T_PATIENT, (LPCTSTR)m_pPatData->strPatientKey, pDataSet, szErrorMsg);
		BCDBClearDataSet(pDataSet, szErrorMsg);
//	}
	
//	if(_nDlgType == PATIENT_INFO_EDIT_STUDY){
		BCDBCreateDataSet(&pDataSet, szErrorMsg);
		BCDBCreateDataElem(&pDataElem, T_STUDY_HOSPNO, m_pPatData->strHospNo.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strHospNo, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_STUDY_CLINICNO, m_pPatData->strClinicNo.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strClinicNo, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_STUDY_BEDNO, m_pPatData->strBedNo.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strBedNo, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;

		BCDBCreateDataElem(&pDataElem, T_STUDY_PHYSICAL, m_pPatData->strPhysical.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strPhysical, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_STUDY_DR, m_pPatData->strDR.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strDR, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_STUDY_DEPART, m_pPatData->strDepart.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strDepart, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;

		BCDBCreateDataElem(&pDataElem, T_STUDY_DATE, strStudyDate.GetLength(), DT_DATE, (void*)(LPCTSTR)strStudyDate, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;

		if(strStudyTime.GetLength() > 0){
			BCDBCreateDataElem(&pDataElem, T_STUDY_TIME, strStudyTime.GetLength(), DT_TIME, (void*)(LPCTSTR)strStudyTime, szErrorMsg);
			pDataSet->push_back(pDataElem); pDataElem = NULL;
		}
		BCDBCreateDataElem(&pDataElem, T_STUDY_DESC, m_pPatData->strMemo.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strMemo, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
//		BCDBCreateDataElem(&pDataElem, T_STUDY_DATA, strFilePathName.GetLength(), DT_STRING, (void*)(LPCTSTR)strFilePathName, szErrorMsg);
//		pDataSet->push_back(pDataElem); pDataElem = NULL;

		bRet = BCDBUpdateDBData(&DBConn, T_STUDY, (LPCTSTR)m_pPatData->strStudyID, pDataSet, szErrorMsg);
		BCDBClearDataSet(pDataSet, szErrorMsg);
	}
	if(_nDlgType == PATIENT_INFO_ADD_PATIENT || _nDlgType == PATIENT_INFO_ADD_PATIENTSTUDY){
		BCDBCreateDataElem(&pDataElem, T_PATIENT_KEY, m_pPatData->strPatientKey.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strPatientKey, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;

		BCDBDataSets* pQueryResult = NULL;
		BCDBCreateDataSets(&pQueryResult, szErrorMsg);
		bRet = BCDBQueryDBData(&DBConn, T_PATIENT, pDataSet, &pQueryResult, szErrorMsg);
		if (pQueryResult->size() <= 0) {
			BCDBCreateDataElem(&pDataElem, T_PATIENT_ID, m_pPatData->strPatientID.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strPatientID, szErrorMsg);
			pDataSet->push_back(pDataElem); pDataElem = NULL;
			
			BCDBCreateDataElem(&pDataElem, T_PATIENT_NAME, m_pPatData->strName.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strName, szErrorMsg);
			pDataSet->push_back(pDataElem); pDataElem = NULL;
			BCDBCreateDataElem(&pDataElem, T_PATIENT_BIRTHDAY, strBirthday.GetLength(), DT_DATE, (void*)(LPCTSTR)strBirthday, szErrorMsg);
			pDataSet->push_back(pDataElem); pDataElem = NULL;
			BCDBCreateDataElem(&pDataElem, T_PATIENT_AGE, strAge.GetLength(), DT_STRING, (void*)(LPCTSTR)strAge, szErrorMsg);
			pDataSet->push_back(pDataElem); pDataElem = NULL;
			BCDBCreateDataElem(&pDataElem, T_PATIENT_WEIGHT, m_pPatData->strWeight.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strWeight, szErrorMsg);
			pDataSet->push_back(pDataElem); pDataElem = NULL;
			BCDBCreateDataElem(&pDataElem, T_PATIENT_HEIGHT, m_pPatData->strHeight.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strHeight, szErrorMsg);
			pDataSet->push_back(pDataElem); pDataElem = NULL;
			BCDBCreateDataElem(&pDataElem, T_PATIENT_BP, m_pPatData->strBP.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strBP, szErrorMsg);
			pDataSet->push_back(pDataElem); pDataElem = NULL;
			BCDBCreateDataElem(&pDataElem, T_PATIENT_TEL, m_pPatData->strTel.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strTel, szErrorMsg);
			pDataSet->push_back(pDataElem); pDataElem = NULL;
			BCDBCreateDataElem(&pDataElem, T_PATIENT_ADDRESS, m_pPatData->strAddress.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strAddress, szErrorMsg);
			pDataSet->push_back(pDataElem); pDataElem = NULL;
			BCDBCreateDataElem(&pDataElem, T_PATIENT_POSTCODE, m_pPatData->strPostCode.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strPostCode, szErrorMsg);
			pDataSet->push_back(pDataElem); pDataElem = NULL;
			BCDBCreateDataElem(&pDataElem, T_PATIENT_SEX, m_pPatData->strSex.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strSex, szErrorMsg);
			pDataSet->push_back(pDataElem); pDataElem = NULL;
			bRet = BCDBAddDBData(&DBConn, T_PATIENT, pDataSet, szErrorMsg);
		}

		BCDBClearDataSet(pDataSet, szErrorMsg);
		BCDBClearDataSets(pQueryResult, szErrorMsg);
	}
	
	if(_nDlgType == PATIENT_INFO_ADD_STUDY || _nDlgType == PATIENT_INFO_ADD_PATIENTSTUDY){
		BCDBCreateDataSet(&pDataSet, szErrorMsg);
		BCDBCreateDataElem(&pDataElem, T_PATIENT_KEY, m_pPatData->strPatientKey.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strPatientKey, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_STUDY_HOSPNO, m_pPatData->strHospNo.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strHospNo, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_STUDY_CLINICNO, m_pPatData->strClinicNo.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strClinicNo, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_STUDY_BEDNO, m_pPatData->strBedNo.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strBedNo, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_STUDY_PHYSICAL, m_pPatData->strPhysical.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strPhysical, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_STUDY_DR, m_pPatData->strDR.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strDR, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_STUDY_DEPART, m_pPatData->strDepart.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strDepart, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		
		BCDBCreateDataElem(&pDataElem, T_STUDY_DATE, strStudyDate.GetLength(), DT_DATE, (void*)(LPCTSTR)strStudyDate, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;

		if(strStudyTime.GetLength() > 0){
			BCDBCreateDataElem(&pDataElem, T_STUDY_TIME, strStudyTime.GetLength(), DT_TIME, (void*)(LPCTSTR)strStudyTime, szErrorMsg);
			pDataSet->push_back(pDataElem); pDataElem = NULL;
		}
		BCDBCreateDataElem(&pDataElem, T_STUDY_DESC, m_pPatData->strMemo.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strMemo, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;

		CString strShortTerm = "";
		CIniReadWrite::GetMe()->LoadString(DEF_GENERAL_SETTING, DEF_SHORTTERM, &strShortTerm);
		m_pPatData->strFilePathName = BuildFilePathName(strShortTerm, m_pPatData->strPatientID, strStudyID);
		BCDBCreateDataElem(&pDataElem, T_STUDY_DATA, m_pPatData->strFilePathName.GetLength(), DT_STRING, (void*)(LPCTSTR)m_pPatData->strFilePathName, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;
		BCDBCreateDataElem(&pDataElem, T_STUDY_ID, strStudyID.GetLength(), DT_STRING, (void*)(LPCTSTR)strStudyID, szErrorMsg);
		pDataSet->push_back(pDataElem); pDataElem = NULL;

//		CMainFrame* pMainFrame = (CMainFrame*)(theApp.m_pMainWnd);
//		CString strLoadLanguage = "";
//		CIniReadWrite::GetMe()->LoadString(DEF_GENERAL_SETTING, DEF_GENERAL_LOAD_LANGUAGE, &strLoadLanguage);
//		CString strResult = GetECGStudyResult(&pMainFrame->m_ecgDataDef, strLoadLanguage);
//		BCDBCreateDataElem(&pDataElem, T_STUDY_PROPRESULT, strResult.GetLength(), DT_STRING, (void*)(LPCTSTR)strResult, szErrorMsg);
//		pDataSet->push_back(pDataElem); pDataElem = NULL;

		bRet = BCDBAddDBData(&DBConn, T_STUDY, pDataSet, szErrorMsg);
		BCDBClearDataSet(pDataSet, szErrorMsg);
	}
	bRet = BCDBDisconnectionDB(&DBConn, szErrorMsg);

	EndWaitCursor();

	return TRUE;
}
void CDialogPatInfo::OnOK() 
{
	CString strErrorMsg = "";
	if(!BuildPatientData()){
		strErrorMsg.LoadString(IDS_STRING122);
		MessageBox(strErrorMsg, NULL, MB_OK|MB_ICONERROR);

		return;
	}

	SavePatient();

	SaveInfo();
	
	CDialog::OnOK();
}

void CDialogPatInfo::SaveInfo()
{
	CString strTemp, strField;
	int nNum = 0;
//	CIniReadWrite::GetMe()->LoadString(DEF_STUDY_DEPART, DEF_STUDY_DEPART_NUM, &strTemp);
//	strTemp.TrimLeft(); strTemp.TrimRight();
//	if(strTemp.GetLength() > 0)
//		nNum = atoi((LPCTSTR)strTemp);
//
//	strTemp = "";
//	for (int nLoop = 0; nLoop < nNum; ++nLoop)
//	{
//		strField.Format("%s_%d", DEF_STUDY_DEPART_NAME, nLoop);
//		CIniReadWrite::GetMe()->LoadString(DEF_STUDY_DEPART, strField, &strTemp);
//		m_cbKS.AddString(strTemp);
//		strTemp = "";
//	}
//
//	nNum = 0;
//	CIniReadWrite::GetMe()->LoadString(DEF_STUDY_DR, DEF_STUDY_DR_NUM, &strTemp);
//	strTemp.TrimLeft(); strTemp.TrimRight();
//	if(strTemp.GetLength() > 0)
//		nNum = atoi((LPCTSTR)strTemp);
//	
//	strTemp = "";
//	for (nLoop = 0; nLoop < nNum; ++nLoop)
//	{
//		strField.Format("%s_%d", DEF_STUDY_DR_NAME, nLoop);
//		CIniReadWrite::GetMe()->LoadString(DEF_STUDY_DR, strField, &strTemp);
//		m_cbSZYS.AddString(strTemp);
//		strTemp = "";
//	}
//
//	nNum = 0;
//	CIniReadWrite::GetMe()->LoadString(DEF_STUDY_PHYSICAL, DEF_STUDY_PHYSICAL_NUM, &strTemp);
//	strTemp.TrimLeft(); strTemp.TrimRight();
//	if(strTemp.GetLength() > 0)
//		nNum = atoi((LPCTSTR)strTemp);
//	
//	strTemp = "";
//	for (nLoop = 0; nLoop < nNum; ++nLoop)
//	{
//		strField.Format("%s_%d", DEF_STUDY_PHYSICAL_NAME, nLoop);
//		CIniReadWrite::GetMe()->LoadString(DEF_STUDY_PHYSICAL, strField, &strTemp);
//		m_cbCZYS.AddString(strTemp);
//		strTemp = "";
//	}
//	
	BOOL bFind = FALSE;
	CString strText = "";
	((CComboBox*)GetDlgItem(IDC_COMBO_KS))->GetWindowText(strText);
	if (strText.GetLength() > 0) {
		CIniReadWrite::GetMe()->LoadString(DEF_STUDY_DEPART, DEF_STUDY_DEPART_NUM, &strTemp);
		strTemp.TrimLeft(); strTemp.TrimRight();
		if(strTemp.GetLength() > 0)
			nNum = atoi((LPCTSTR)strTemp);

		if (nNum > 0) {
			strTemp = "";
			for (int nLoop = 0; nLoop < nNum; ++nLoop)
			{
				strField.Format("%s_%d", DEF_STUDY_DEPART_NAME, nLoop);
				CIniReadWrite::GetMe()->LoadString(DEF_STUDY_DEPART, strField, &strTemp);
				if (!strText.CompareNoCase(strTemp)) {
					bFind = TRUE;

					break;
				}
			}

			if (!bFind) {
				strTemp.Format("%d", nNum+1);
				CIniReadWrite::GetMe()->SaveString(DEF_STUDY_DEPART, DEF_STUDY_DEPART_NUM, strTemp);
				strField.Format("%s_%d", DEF_STUDY_DEPART_NAME, nNum);
				CIniReadWrite::GetMe()->SaveString(DEF_STUDY_DEPART, strField, strText);
			}
		}else{
			strTemp.Format("%d", 1);
			CIniReadWrite::GetMe()->SaveString(DEF_STUDY_DEPART, DEF_STUDY_DEPART_NUM, strTemp);
			strField.Format("%s_%d", DEF_STUDY_DEPART_NAME, 0);
			CIniReadWrite::GetMe()->SaveString(DEF_STUDY_DEPART, strField, strText);
		}
		strText = "";
	}

	bFind = FALSE;
	((CComboBox*)GetDlgItem(IDC_COMBO_SZYS))->GetWindowText(strText);
	if (strText.GetLength() > 0) {
		CIniReadWrite::GetMe()->LoadString(DEF_STUDY_DR, DEF_STUDY_DR_NUM, &strTemp);
		strTemp.TrimLeft(); strTemp.TrimRight();
		if(strTemp.GetLength() > 0)
			nNum = atoi((LPCTSTR)strTemp);

		if (nNum > 0) {
			strTemp = "";
			for (int nLoop = 0; nLoop < nNum; ++nLoop)
			{
				strField.Format("%s_%d", DEF_STUDY_DR_NAME, nLoop);
				CIniReadWrite::GetMe()->LoadString(DEF_STUDY_DR, strField, &strTemp);
				if (!strText.CompareNoCase(strTemp)) {
					bFind = TRUE;

					break;
				}
			}

			if (!bFind) {
				strTemp.Format("%d", nNum+1);
				CIniReadWrite::GetMe()->SaveString(DEF_STUDY_DR, DEF_STUDY_DR_NUM, strTemp);
				strField.Format("%s_%d", DEF_STUDY_DR_NAME, nNum);
				CIniReadWrite::GetMe()->SaveString(DEF_STUDY_DR, strField, strText);
			}
		}else{
			strTemp.Format("%d", 1);
			CIniReadWrite::GetMe()->SaveString(DEF_STUDY_DR, DEF_STUDY_DR_NUM, strTemp);
			strField.Format("%s_%d", DEF_STUDY_DR_NAME, 0);
			CIniReadWrite::GetMe()->SaveString(DEF_STUDY_DR, strField, strText);
		}
		
		strText = "";
	}
	
	bFind = FALSE;
	((CComboBox*)GetDlgItem(IDC_COMBO_CZYS))->GetWindowText(strText);
	if (strText.GetLength() > 0) {
		CIniReadWrite::GetMe()->LoadString(DEF_STUDY_PHYSICAL, DEF_STUDY_PHYSICAL_NUM, &strTemp);
		strTemp.TrimLeft(); strTemp.TrimRight();
		if(strTemp.GetLength() > 0)
			nNum = atoi((LPCTSTR)strTemp);

		if (nNum > 0) {
			strTemp = "";
			for (int nLoop = 0; nLoop < nNum; ++nLoop)
			{
				strField.Format("%s_%d", DEF_STUDY_PHYSICAL_NAME, nLoop);
				CIniReadWrite::GetMe()->LoadString(DEF_STUDY_PHYSICAL, strField, &strTemp);
				if (!strText.CompareNoCase(strTemp)) {
					bFind = TRUE;

					break;
				}
			}

			if (!bFind) {
				strTemp.Format("%d", nNum+1);
				CIniReadWrite::GetMe()->SaveString(DEF_STUDY_PHYSICAL, DEF_STUDY_PHYSICAL_NUM, strTemp);
				strField.Format("%s_%d", DEF_STUDY_PHYSICAL_NAME, nNum);
				CIniReadWrite::GetMe()->SaveString(DEF_STUDY_PHYSICAL, strField, strText);
			}
		}else{
			strTemp.Format("%d", 1);
			CIniReadWrite::GetMe()->SaveString(DEF_STUDY_PHYSICAL, DEF_STUDY_PHYSICAL_NUM, strTemp);
			strField.Format("%s_%d", DEF_STUDY_PHYSICAL_NAME, 0);
			CIniReadWrite::GetMe()->SaveString(DEF_STUDY_PHYSICAL, strField, strText);
		}
		
		strText = "";
	}
}

void CDialogPatInfo::OnCancel() 
{
	CDialog::OnCancel();
}

BOOL CDialogPatInfo::InitCard()
{
	int nCardPort = -1;
	CIniReadWrite::GetMe()->LoadInt(DEF_GENERAL_SETTING, DEF_CARD_PORT, &nCardPort);
	if (nCardPort < 0) {
		return FALSE;
	}

	icdev = dc_init(nCardPort, 115200);
	if ((int)icdev < 0) {
//		AfxMessageBox("error dc_init");
		return FALSE;
	}

	//AfxMessageBox("Init card succeed.");

	return TRUE;
}

BOOL CDialogPatInfo::DestoryCard()
{
   if (icdev != NULL) {
	   dc_reset(icdev, 10);
	   dc_exit(icdev);
	   icdev = NULL;
   }

   //AfxMessageBox("Destory card succeed.");

   return TRUE;
}

CString CDialogPatInfo::ReadCard()
{
	int nRet = -1;
	int Sector   = 0 ;
    int Address  = 2 ;

	CString strRet = "";
	nRet = dc_disp_str(icdev, "123.45");
	if (nRet != 0) {
		//AfxMessageBox("error dc_disp_str");
		return strRet;
	}

	nRet = dc_card(icdev, 1, &cardsnr);
	if (nRet != 0) {
		//AfxMessageBox("error dc_card");
		return strRet;
	}

	nRet = dc_authentication(icdev, 0, Sector);
	if (nRet != 0) {
		//AfxMessageBox("error dc_authentication");
		return strRet;
	}

	char cReadData[32];
	memset(cReadData, 0, 32);
	nRet = dc_read_hex(icdev, Address, cReadData);
	if (nRet != 0) {
		//AfxMessageBox("error dc_read_hex");
		return strRet;
	}

	char cRealData[10];
	memset(cRealData, 0, 10);
	//strncpy(cRealData, cReadData, 10);
	memcpy((void*)cRealData, (const void*)cReadData, 10);
	cRealData[10] = '\0';
	strRet.Format("%s", cRealData);

	nRet = dc_halt(icdev);
	if (nRet != 0) {
		//AfxMessageBox("error dc_halt");
		return strRet;
	}

	nRet = dc_beep(icdev, 10);
	if (nRet != 0) {
		//AfxMessageBox("error dc_beep.");
		return strRet;
	}

	//AfxMessageBox("Read card succeed.");

	return strRet;
}

void CDialogPatInfo::OnTimer(UINT nIDEvent) 
{
	CString strPatientID = "";
	strPatientID = ReadCard();
	if (strPatientID.GetLength() > 0) {
		((CEdit*)GetDlgItem(IDC_EDIT_CHNO))->SetWindowText(strPatientID);
	}
	
	CDialog::OnTimer(nIDEvent);
}

void CDialogPatInfo::OnDestroy() 
{
	CDialog::OnDestroy();
	
	if (_nDlgType == PATIENT_INFO_ADD_PATIENT || _nDlgType == PATIENT_INFO_ADD_PATIENTSTUDY) {
		KillTimer(uReadCardTimer);
		DestoryCard();
	}

	SaveInputInfo();
}

void CDialogPatInfo::OnDatetimechangeDatetimepickerBirthday(NMHDR* pNMHDR, LRESULT* pResult) 
{
	CString strBirthday = "", strAge = "";
	COleDateTime tmBirthday;
	((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_BIRTHDAY))->GetTime(tmBirthday);
	strBirthday.Format("%04d%02d%02d", tmBirthday.GetYear(), tmBirthday.GetMonth(), tmBirthday.GetDay());
	int nAge = 0;
	COleDateTime tmCurrent = COleDateTime::GetCurrentTime();
	nAge = tmCurrent.GetYear() - tmBirthday.GetYear();
	strAge.Format("%d", nAge);
	if (strAge.GetLength() > 0) {
		((CEdit*)GetDlgItem(IDC_EDIT_AGE))->SetWindowText(strAge);
	}

	*pResult = 0;
}

void CDialogPatInfo::OnChangeEditAge() 
{
	CString strAge = "";	
	((CEdit*)GetDlgItem(IDC_EDIT_AGE))->GetWindowText(strAge);
	if(strAge.GetLength() <= 0) return;

	int nAge = atoi((LPCTSTR)strAge);
	if(nAge >2011)
	{
		GetDlgItem(IDC_EDIT_AGE)->SetWindowText("0");
		nAge =0;
	}

	COleDateTime tmBirthday;
	((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_BIRTHDAY))->GetTime(tmBirthday);
	COleDateTime tmCurrent = COleDateTime::GetCurrentTime();
	tmBirthday.SetDate(tmCurrent.GetYear() - nAge, tmBirthday.GetMonth(), tmBirthday.GetDay());

	((CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_BIRTHDAY))->SetTime(tmBirthday);
}

void CDialogPatInfo::OnCheckSex() 
{
	m_chkSex.GetCheck() ? ((CEdit*)GetDlgItem(IDC_COMBO_SEX))->EnableWindow(TRUE) : ((CEdit*)GetDlgItem(IDC_COMBO_SEX))->EnableWindow(FALSE);	
}

void CDialogPatInfo::OnCheckAddress() 
{
	m_chkAddress.GetCheck() ? ((CEdit*)GetDlgItem(IDC_EDIT_PATIENT_ADDRESS))->EnableWindow(TRUE) : ((CEdit*)GetDlgItem(IDC_EDIT_PATIENT_ADDRESS))->EnableWindow(FALSE);	
}

void CDialogPatInfo::OnCheckAge() 
{
	m_chkAge.GetCheck() ? ((CEdit*)GetDlgItem(IDC_EDIT_AGE))->EnableWindow(TRUE) : ((CEdit*)GetDlgItem(IDC_EDIT_AGE))->EnableWindow(FALSE);	
}

void CDialogPatInfo::OnCheckBedno() 
{
	m_chkBedNo.GetCheck() ? ((CEdit*)GetDlgItem(IDC_EDIT_BEDNO))->EnableWindow(TRUE) : ((CEdit*)GetDlgItem(IDC_EDIT_BEDNO))->EnableWindow(FALSE);	
}

void CDialogPatInfo::OnCheckBirthday() 
{
	m_chkBirthday.GetCheck() ? ((CEdit*)GetDlgItem(IDC_DATETIMEPICKER_BIRTHDAY))->EnableWindow(TRUE) : ((CEdit*)GetDlgItem(IDC_DATETIMEPICKER_BIRTHDAY))->EnableWindow(FALSE);	
}

void CDialogPatInfo::OnCheckBp() 
{
	m_chkBP.GetCheck() ? ((CEdit*)GetDlgItem(IDC_EDIT_BP))->EnableWindow(TRUE) : ((CEdit*)GetDlgItem(IDC_EDIT_BP))->EnableWindow(FALSE);	
}

void CDialogPatInfo::OnCheckClinicno() 
{
	m_chkClinicNo.GetCheck() ? ((CEdit*)GetDlgItem(IDC_EDIT_CLINICNO))->EnableWindow(TRUE) : ((CEdit*)GetDlgItem(IDC_EDIT_CLINICNO))->EnableWindow(FALSE);	
}

void CDialogPatInfo::OnCheckCzys() 
{
	m_chkCZYS.GetCheck() ? ((CEdit*)GetDlgItem(IDC_COMBO_CZYS))->EnableWindow(TRUE) : ((CEdit*)GetDlgItem(IDC_COMBO_CZYS))->EnableWindow(FALSE);	
}

void CDialogPatInfo::OnCheckHeight() 
{
	m_chkHeight.GetCheck() ? ((CEdit*)GetDlgItem(IDC_EDIT_HEIGHT))->EnableWindow(TRUE) : ((CEdit*)GetDlgItem(IDC_EDIT_HEIGHT))->EnableWindow(FALSE);	
}

void CDialogPatInfo::OnCheckHospno() 
{
	m_chkHospNo.GetCheck() ? ((CEdit*)GetDlgItem(IDC_EDIT_HOSPNO))->EnableWindow(TRUE) : ((CEdit*)GetDlgItem(IDC_EDIT_HOSPNO))->EnableWindow(FALSE);	
}

void CDialogPatInfo::OnCheckKs() 
{
	m_chkKS.GetCheck() ? ((CEdit*)GetDlgItem(IDC_COMBO_KS))->EnableWindow(TRUE) : ((CEdit*)GetDlgItem(IDC_COMBO_KS))->EnableWindow(FALSE);	
}

void CDialogPatInfo::OnCheckMemo() 
{
	m_chkMemo.GetCheck() ? ((CEdit*)GetDlgItem(IDC_EDIT_MEMO))->EnableWindow(TRUE) : ((CEdit*)GetDlgItem(IDC_EDIT_MEMO))->EnableWindow(FALSE);	
}

void CDialogPatInfo::OnCheckPostcode() 
{
	m_chkPostCode.GetCheck() ? ((CEdit*)GetDlgItem(IDC_EDIT_PATIENT_POSTCODE))->EnableWindow(TRUE) : ((CEdit*)GetDlgItem(IDC_EDIT_PATIENT_POSTCODE))->EnableWindow(FALSE);	
}

void CDialogPatInfo::OnCheckSzys() 
{
	m_chkSZYS.GetCheck() ? ((CEdit*)GetDlgItem(IDC_COMBO_SZYS))->EnableWindow(TRUE) : ((CEdit*)GetDlgItem(IDC_COMBO_SZYS))->EnableWindow(FALSE);	
}

void CDialogPatInfo::OnCheckTel() 
{
	m_chkTel.GetCheck() ? ((CEdit*)GetDlgItem(IDC_EDIT_TEL))->EnableWindow(TRUE) : ((CEdit*)GetDlgItem(IDC_EDIT_TEL))->EnableWindow(FALSE);	
}

void CDialogPatInfo::OnCheckWeight() 
{
	m_chkWeight.GetCheck() ? ((CEdit*)GetDlgItem(IDC_EDIT_WEIGHT))->EnableWindow(TRUE) : ((CEdit*)GetDlgItem(IDC_EDIT_WEIGHT))->EnableWindow(FALSE);	
}
